package domain.models;

import javax.swing.AbstractListModel;

import controllers.DomainController;
import controllers.MyObserver;

public class MyListModel extends AbstractListModel implements MyObserver{

	private String[] data;
	private DomainController domainController;

	public MyListModel(DomainController domainController) {
		this.domainController = domainController;
		this.data = domainController.getPlayersToBePaired();
		domainController.getCurrentTournament().addObserver(this);
	}

	@Override
	public String getElementAt(int index) {
		return data[index];
	}
	
	// To test, approach (String[]? String[][]?) is discussable
	public String[][] getData()
	{
		String[][] formattedData = new String[data.length][2];
		String spaceDelimiter = " ";
		String[] formattedName;
		
		for(int counter = 0; counter < data.length; counter++)
		{
			formattedName = data[counter].split(spaceDelimiter);
			formattedData[counter][0] = formattedName[0];
			formattedData[counter][1] = formattedName[1];
		}
		
		return formattedData;
	}

	@Override
	public int getSize() {
		return data.length;
	}

	@Override
	public void update(String adressee) {
		this.data = domainController.getPlayersToBePaired();
		fireContentsChanged(data, 0, data.length);
	}
}
